package com.zyj.test.month1.day12;

import com.zyj.test.ListNode;

import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @author Yijia Zhang
 * @create 2024-05-29 23:39
 */
public class code0023 {
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> pq = new PriorityQueue<>(Comparator.comparingInt(l -> l.val));
        for (ListNode node : lists) {
            if (node != null)
                pq.offer(node);
        }
        ListNode dummy = new ListNode(-1);
        ListNode tmp = dummy;
        while (pq.size() > 0) {
            ListNode poll = pq.poll();
            ListNode next = poll.next;
            poll.next = null;
            tmp.next = poll;
            if (next != null) {
                pq.offer(next);
            }
            tmp = tmp.next;
        }
        return dummy.next;

    }
}
